package algorithm.tree;

/**
 * 输出二叉树指定结点的所有祖先结点
 */
public class Ancestors {
    public static void main(String[] args) {
        //标明空子树的先根序列
        String[] preorder = {"A","B","D",null,"G",null,null,null,"C","E",null,null,"F","H"};
        BinaryTree<String> binaryTree = new BinaryTree<String>(preorder);
        binaryTree.preOrder();
        String value = "H";
        BinaryNode<String> find = binaryTree.search(value);
        if(find == null)
            System.out.println("未找到");
        else {
            BinaryNode<String> parent = binaryTree.getParent(find);
            System.out.println("\n"+find.data+"的祖先结点是");
            while (parent!=null){
                System.out.println(parent.data+" ");
                parent = binaryTree.getParent(parent);
            }
        }
    }
}
